// pages/business/businessDetails/businessDetails.js
var utils = require('../../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var codeName = getApp().globalData.codeName;
var userId = getApp().globalData.userId;
var WxParse = require('../../../wxParse/wxParse.js');
//ID删除发布信息
function del(that) {
  utils.http(httpUrl + "deletePublish", {
    code: codeName,
    userid: userId,
    cateid: that.data.id,
    houseid: that.data.idd
  }, function (res) {
    //console.log(res);
    that.getLoad();
  })
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected: true,
    selected1: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;

    utils.http(httpUrl + "getmerchantinfo", {
      code: codeName,
      merchantid: options.id,
      userid:userId
    }, function (res) {
      console.log(res);
      WxParse.wxParse('article_content', 'html', res.getmerchantinfo.productShl, that, 5)
      that.setData({
        item: res.getmerchantinfo
      })
      console.log(that.data.item.name)
         wx.setNavigationBarTitle({
           title: that.data.item.name
    })
    })
   
 
    that.getLoad();
  },
  selected: function (e) {
    this.setData({
      selected1: false,
      selected: true
    })
  },
  selected1: function (e) {
    this.setData({
      selected: false,
      selected1: true
    })
  },

  //收藏切换
  oNcollectbyTap: function (e) {
    var that = this;
    that.data.item.collect = !that.data.item.collect;
    if (that.data.item.collect) {
      console.log(that.data.item.collect);
      utils.http(httpUrl + "manageCollect", {
        code: codeName,
        merchantid: that.data.item.id,
        userid: userId
      }, function (res) {
        if(res.status==200){
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 2000
          })
        }
        that.setData({
          item: that.data.item
        })
      })
    } else {
      console.log(that.data.item.collect);
      utils.http(httpUrl + "manageCollect", {
        code: codeName,
        cateid: that.data.item.id,
        userid: userId
      }, function (res) {
        wx.showToast({
          title: '取消收藏',
          icon: 'success',
          duration: 2000
        })
        that.setData({
          item: that.data.item
        })
      })
    }
  },

  getLoad: function (e) {
    var that = this;
    utils.http(httpUrl + "getUserPublish", {
      code: codeName,
      userid: userId,
      cityid: wx.getStorageSync("city").cityid
    }, function (res) {
    
      var cateinfolist = [];
      for (var index in res.cate) {
        console.log(res.cate);
        if (res.cate[index].content == undefined){
          res.cate[index].content=""
        }
        //文字截取
        var sub = res.cate[index].content.substring(0, 40);
        var subContent = sub + "...";
        that.setData({
          advlist: res.cate[index].advlist
        })
        that.data.advlist.length = 3;
        res.cate[index].collect = !res.cate[index].collect;

        var catePost = {
          advlist: that.data.advlist,
          subAdvlist: res.cate[index].advlist, //截取后的图片
          city: res.cate[index].city,
          code: res.cate[index].code,
          collectby: res.cate[index].collectby,
          content: res.cate[index].content,
          subContent: subContent,//截取后的属性
          createTime: res.cate[index].createTime,
          id: res.cate[index].id,
          index: res.cate[index].index,
          collect: res.cate[index].collect,
          ischoiceness: res.cate[index].ischoiceness,
          istop: res.cate[index].istop,
          lookby: res.cate[index].lookby,
          publishby: res.cate[index].publishby,
          title: res.cate[index].title,
          type: res.cate[index].type,
          user: res.cate[index].user,
          isstare: false

        }
        cateinfolist.push(catePost);

      }
      that.setData({
        cate: cateinfolist,
        house: res.house,
        user: res.user
      })

      console.log(that.data.cate);

    })
  },
  //全文和收起
  noStareTap: function (e) {
    var index = e.currentTarget.dataset.index;
    this.data.cate[index].isstare = !this.data.cate[index].isstare;
    this.setData({
      cate: this.data.cate
    })
  },
  //删除美食
  deltypemeishitap: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      id: id
    })
    del(this);

  },

  //删除房屋
  deltypejiajutap: function (e) {
    var idd = e.currentTarget.dataset.idd;
    this.setData({
      idd: idd
    })
    del(this);

  },

})